package com.librarysys.controller;

import com.librarysys.common.Const;
import com.librarysys.common.Result;
import com.librarysys.entity.Library;
import com.librarysys.entity.Manager;
import com.librarysys.service.IOtherService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by yuhui on 2017/7/11.
 */
@Controller
public class OtherController {

    @Autowired
    private IOtherService iOtherService;

    /**
     * 获得图书馆信息
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/library/get")
    public String getLibrary(@RequestParam(defaultValue = "1") Integer id, HttpServletRequest request){
        Result library = iOtherService.getLibrary(id);
        request.setAttribute("library",library.getData());
        return "library_modify";
    }

    /**
     * 保存图书馆信息
     * @param library
     * @param request
     * @param createDate
     * @return
     */
    @RequestMapping("/library/save")
    public String saveLibrary(Library library,HttpServletRequest request,String createDate){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = sdf.parse(createDate);
            library.setCreatedate(parse);
        } catch (ParseException e) {
            request.setAttribute("msg","时间格式输入错误");
        }
        library.setId(1);
        Result result = iOtherService.saveLibrary(library);
        request.setAttribute("msg",result.getMsg());
        return "library_modify";
    }

    /**
     * 首页，根据借阅次数进行排名
     * @param request
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        Result index = iOtherService.index();
        request.setAttribute("book",index.getData());
        return "main";
    }

    @RequestMapping("/parameter/get")
    public String parameter(Model model){
        Result parameter = iOtherService.getParameter();
        model.addAttribute("param",parameter.getData());
        return "parameter_modify";
    }

    @RequestMapping("/parameter/save")
    public String parameterSave(HttpServletRequest request,Integer cost,Integer validity){
        Manager attribute = (Manager) request.getSession().getAttribute(Const.CURRENT_USER);
        if(attribute==null || attribute.getRole()!=Const.Role.SUPER_ADMIN){
            request.setAttribute("msg","没有权限");
        }else{
            Result result = iOtherService.saveParameter(cost, validity);
            request.setAttribute("msg",result.getMsg());
        }
        return "/parameter/get";
    }
    @RequestMapping("/bookcase/list")
    public String bookcaseList(Model model){
        Result result = iOtherService.listBookcase();
        model.addAttribute("bookcases",result.getData());
        return "bookcase";
    }


}
